package jmeterBusiness;

import models.MyConfig;
import models.ReturnJson;
import models.TestServerBusinessImpl;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Properties;

/**
 * Created by yangxianming on 2017/7/7.
 * 命令行格式：//D:\xxxx\apache-jmeter-3.1\bin\jmeter -n -t D:\xxxxx\appList.jmx -l D:\xxxx\appList.jtl -e -o D:\xxxx\appList
 */
public class RunJmx implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        String projectName = request.getParameter("projectName");
        String scriptName = request.getParameter("scriptName");
        String action = request.getParameter("action");
        Properties properties = myConfig.getPropertiesConfig().getProperties();

        //检查jmeter是否在运行
        if (Objects.equals("isRun", action)){
            if (Objects.equals(myConfig.getNo3Factory().getCommand(), "")){
                returnJson.setApiStatus(0);
                returnJson.setResult("stop");
            }else {
                returnJson.setApiStatus(1);
                returnJson.setResult("running");
            }
            returnJson.setApiStatus(1);
            return returnJson.toString();
        }

        //如果发现有在执行的程序，就跳出去
        if (!Objects.equals(myConfig.getNo3Factory().getCommand(), "")){
            returnJson.setApiStatus(0);
            returnJson.setErrorMsg("jmeter running,can not run new jmeter process");
            return returnJson.toString();
        }

        // --组装命令行--
        String command;
//        String appPath = properties.getProperty("appPath");
        String appPath = myConfig.getAppPath();
        String jmeterBinPath = properties.getProperty("jmeterBinPath");
        String jmeterScriptPath = properties.getProperty("jmeterScriptPath");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("_yyyyMMdd_HHmmss");
        String currentTime = simpleDateFormat.format(date);

        //获取系统名字
        String startCmd = myConfig.getSplitMark("startCmd");
        String splitMark = myConfig.getSplitMark("splitMark");

        //jmeter基本命令
        String fullScrPath = appPath + jmeterScriptPath + splitMark;
        command = startCmd + appPath + jmeterBinPath + splitMark + "jmeter -n -t ";
        command = command + fullScrPath + splitMark + scriptName + ".jmx";
        command = command + " -l " + fullScrPath + splitMark + scriptName + currentTime + ".jtl";
        command = command + " -e -o " +fullScrPath + splitMark + scriptName + currentTime;

        //检查脚本文件是否存在
        File file = new File(fullScrPath + splitMark + scriptName + ".jmx");
        if (!file.exists()){
            returnJson.setApiStatus(0);
            returnJson.setErrorMsg("jmeter file " + scriptName + " is not exists!!");
            return returnJson.toString();
        }

        // --调用测试--
        No3Factory no3Factory = new No3Factory();
        no3Factory.setCommand(command);
        myConfig.setNo3Factory(no3Factory);
        no3Factory.start();

        returnJson.setApiStatus(1);
        returnJson.setResult("ok");
        return returnJson.toString();
    }

}
